const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
	const wxContext = cloud.getWXContext()
	try {
		let result = {};
		switch (event.action) {
			case 'msgSecCheck':
				//文本校验
				result = await cloud.openapi.security.msgSecCheck({
					content: event.content
				});
				break;
			case 'imgSecCheck':
				//图片校验
				result = await cloud.openapi.security.imgSecCheck({
					media: {
						contentType: 'image/jpg',
						value: Buffer.from(event.content)
					}
				});
				break;
			default:
				result = {
					code: 403,
					msg: '非法访问'
				}
				break;
		}
		if (result && result.errCode.toString() === '87014') {
			return {
				code: 300,
				msg: '内容含有违法违规内容',
				data: result,
				content: event.content
			}
		} else {
			return {
				code: 200,
				msg: 'ok',
				data: result,
				content: event.content
			}
		}
	} catch (err) {
    if(err.errCode == 87014){
      return {
        code: 300,
        msg: '内容含有违法违规内容',
        data: err,
        content: event.content
      }
    }else{
      return {
        code: 400,
        msg: '调用security接口异常',
        data: err,
        content: event.content
      }
    }
		
	}
}
